/**
 * 1052. 爱生气的书店老板
 * https://leetcode-cn.com/problems/grumpy-bookstore-owner/
 */
public class Solutions_1052 {
    public static void main(String[] args) {
        int[] customers = {1, 0, 1, 2, 1, 1, 7, 5};
        int[] grumpy    = {0, 1, 0, 1, 0, 1, 0, 1};
        int X = 3;  // output: 16
//        int X = 1;  // output: 15

        int result = maxSatisfied(customers, grumpy, X);
        System.out.println(result);
    }

    public static int maxSatisfied(int[] customers, int[] grumpy, int X) {
        int base = 0;
        int len = customers.length;
        for (int i = 0; i < len; i++) {
            if (grumpy[i] == 0) {
                base += customers[i];
                // 方便后续操作：改变为 0，反正已经累加到 base 中了
                customers[i] = 0;
            }
        }
        int res = base;
        // i 作为窗口的右侧指针，往右移动
        for (int i = 0; i < len; i++) {
            base += customers[i];
            if (i >= X) {
                // 收缩窗口左侧指针，值也相应减少
                base -= customers[i - X];
            }
            res = Math.max(res, base);
        }
        return res;
    }
}
